<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入订单描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="skuName">
        <el-input
          v-model="queryParams.skuName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="handlePay"
        >一键付款</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >一键删除</el-button>
      </el-col>



      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="id" />
      <el-table-column label="订单类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="no" />
      <el-table-column label="订单总价" align="center" prop="price" />
      <el-table-column label="订单描述" align="center" prop="description" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户名字" align="center" prop="userName" />
      <el-table-column label="商品id" align="center" prop="skuId" />
      <el-table-column label="商品名称" align="center" prop="skuName" />
      <el-table-column label="商品数量" align="center" prop="skuNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePay(scope.row)"
            v-hasPermi="['business:order:edit']"
          >付款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或处理订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <!--共计-->
      <div> {{this.price}} </div>
      <img src="../img.png" style="width: 500px;height: 500px;" alt="付款二维码">
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或处理订单对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="400px" append-to-body>
      <!--共计-->
      <div> {{this.price}} </div>
      <img src="../img.png" style="width: 500px;height: 500px;" alt="付款二维码">
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="open2=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/business/order";

export default {
  name: "Order",
  dicts: ['order_type'],
  data() {
    return {
      payList:[],
      price: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        no: null,
        price: null,
        description: null,
        userId: null,
        userName: null,
        skuId: null,
        skuName: null,
        skuNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.queryParams.userId = this.$store.getters.userId;
      this.queryParams.userName = this.$store.getters.name;
      this.queryParams.type = 1;
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        no: null,
        price: null,
        description: null,
        userId: null,
        userName: null,
        skuId: null,
        skuName: null,
        skuNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handlePay(row) {
      const ids = row.id || this.ids;
      if (row.id!=null){
        this.payList = this.orderList.filter(item => ids === item.id)
        // 计算data 数组 中的price
        this.price = row.price
        this.open  = true;
        this.title = "请付款";
      }
      this.payList = this.orderList.filter(item => ids.concat(item.id))
      // 计算data 数组 中的price
      this.price = this.payList.reduce((acc, cur) => {
        return acc + cur.price
      }, 0)
      this.open = true;
      this.title = "请付款";
    },
    /** 提交按钮 */
    submitForm() {
      this.payList.map(item => {
        item.type = 2;
        updateOrder(item);
      })
      this.$message.success("付款成功");
      this.getList();
      this.open = false;
    },
  }
};
</script>
